package com.nic.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nic.view.R;
import com.nic.view.util.Utility;

/**
 * 带修改删除的列表
 * Created by Administrator on 2018/11/16.
 */

public abstract class BusinessAdapter<T> extends CommonAdapter<T> {
    public static final int VIEW_ALL_VISIBLE = 0;//全部显示
    public static final int VIEW_DELETE_VISIBLE = 1;//显示删除
    public static final int VIEW_UPDATE_VISIBLE = 2;//显示修改
    public static final int VIEW_ALL_INVISIBLE = 3;//全部隐藏
    private int visibility;//0全部显示1显示删除2显示修改3全部隐藏

    public void setVisibility(int visibility) {
        this.visibility = visibility;
        notifyDataSetChanged();
    }

    @Override
    public View getBindView(int position, View convertView, ViewGroup parent) {
        BusinessHolder businessHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_business, null);
            businessHolder = new BusinessHolder(convertView);
            setShowState(businessHolder);
            convertView.setTag(businessHolder);
        } else {
            businessHolder = (BusinessHolder) convertView.getTag();
        }
        final T t = getList().get(position);
        addContent(parent, businessHolder);
        deleteClick(position, businessHolder, t);
        contentClick(position, businessHolder, t);
        updateClick(position, businessHolder, t);
        contentView(businessHolder, t, position);
        return convertView;
    }

    private void setShowState(BusinessHolder businessHolder) {
        if (visibility == VIEW_DELETE_VISIBLE) {
            businessHolder.tvDelete.setVisibility(View.VISIBLE);
        } else if (visibility == VIEW_UPDATE_VISIBLE) {
            businessHolder.tvUpdate.setVisibility(View.VISIBLE);
        } else if (visibility == VIEW_ALL_INVISIBLE) {
            businessHolder.tvDelete.setVisibility(View.GONE);
            businessHolder.tvUpdate.setVisibility(View.GONE);
        } else {
            businessHolder.tvDelete.setVisibility(View.VISIBLE);
            businessHolder.tvUpdate.setVisibility(View.VISIBLE);
        }
    }

    private void addContent(ViewGroup parent, BusinessHolder businessHolder) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(getContentViewId(), null);
        LinearLayout.LayoutParams item_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        businessHolder.llContent.removeAllViewsInLayout();
        businessHolder.llContent.addView(itemView, item_params);
        int width = Utility.getScreenWidth(parent.getContext());
        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.WRAP_CONTENT);
        businessHolder.llContent.setLayoutParams(params);
    }

    private void updateClick(final int position, final BusinessHolder businessHolder, final T t) {

        businessHolder.tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                businessHolder.hScroll.fullScroll(View.FOCUS_LEFT);
                update(v, t, position);
            }
        });
    }

    private void contentClick(final int position, final BusinessHolder businessHolder, final T t) {
        businessHolder.llContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick(v, t, position);
            }
        });
    }

    private void deleteClick(final int position, final BusinessHolder businessHolder, final T t) {
        businessHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getList().remove(position);
                notifyDataSetChanged();
                businessHolder.hScroll.fullScroll(View.FOCUS_LEFT);
                delete(v, t, position);
            }
        });
    }

    public abstract int getContentViewId();

    /**
     * @param businessHolder 外层父类传递
     * @param t
     */
    public abstract void contentView(BusinessHolder businessHolder, T t, int position);

    /**
     * 滑动删除
     *
     * @param v
     */
    public abstract void delete(View v, T t, int position);

    /**
     * 滑动修改
     *
     * @param v
     */
    public abstract void update(View v, T t, int position);

    /**
     * 点击条目
     *
     * @param v
     */
    public abstract void itemClick(View v, T t, int position);

    public static class BusinessHolder {
        LinearLayout llContent;
        TextView tvUpdate;
        TextView tvDelete;
        HorizontalScrollView hScroll;

        BusinessHolder(View view) {
            llContent = (LinearLayout) view.findViewById(R.id.ll_content);
            tvUpdate = (TextView) view.findViewById(R.id.tv_update);
            tvDelete = (TextView) view.findViewById(R.id.tv_delete);
            hScroll = (HorizontalScrollView) view.findViewById(R.id.hScroll);
        }

        public LinearLayout getLlContent() {
            return llContent;
        }

        public TextView getTvUpdate() {
            return tvUpdate;
        }

        public TextView getTvDelete() {
            return tvDelete;
        }

        public HorizontalScrollView gethScroll() {
            return hScroll;
        }
    }
}
